﻿using LBSoft.Business.NewDAL;
using LBSoft.Business;
using LBSoft.Common.DotNetCode;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using LBSoft.Common.DotNetExt;
using Newtonsoft.Json;
using System.Threading;
using System.Configuration;

namespace SimpleService
{
    public partial class Service1 : ServiceBase
    {
        CommonDal com = new CommonDal();
        Common_Dal systemidao = new Common_Dal();
        LogHelper log = new LogHelper();

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            log.WriteLog("--------------------------服务启动--------------------------");
            Task t1 = Task.Factory.StartNew(SyncExecIntervalSecond);
            Task t2 = Task.Factory.StartNew(SyncExecIntervalMinute);
            Task t3 = Task.Factory.StartNew(SyncExecTimeSpot);
        }

        protected override void OnStop()
        {
            log.WriteLog("--------------------------服务停止--------------------------");
        }

        /// <summary>
        /// 间隔执行（秒）
        /// </summary>
        private void SyncExecIntervalSecond()
        {
            try
            {
                int intervalSecond = ConfigurationManager.AppSettings["IntervalSecond"].ToInt32();
                while (true)
                {
                    #region 执行业务代码

                    ExecIntervalSecondFunc();

                    #endregion

                    Thread.Sleep(1000 * intervalSecond); //每秒*intervalSecond
                }
            }
            catch (Exception ex)
            {
                log.WriteLog("异常：间隔执行（秒）");
                log.WriteExceptionLog(ex);
            }
        }

        /// <summary>
        /// 间隔执行（分）
        /// </summary>
        private void SyncExecIntervalMinute()
        {
            try
            {
                int intervalMinute = ConfigurationManager.AppSettings["IntervalMinute"].ToInt32();
                while (true)
                {
                    #region 执行业务代码

                    ExecIntervalMinuteFunc();

                    #endregion

                    Thread.Sleep(1000 * 60 * intervalMinute); //每分钟*intervalMinute
                }
            }
            catch (Exception ex)
            {
                log.WriteLog("异常：间隔执行（分）");
                log.WriteExceptionLog(ex);
            }
        }

        /// <summary>
        /// 定点执行（时间点）
        /// </summary>
        private void SyncExecTimeSpot()
        {
            try
            {
                string timeSpace = ConfigurationManager.AppSettings["TimeSpot"].ToString2();
                while (true)
                {
                    DateTime Dtime = System.DateTime.Now;
                    string date = Dtime.ToString("HH:mm");

                    if (timeSpace.IndexOf(date) > -1)
                    {
                        #region 执行业务代码

                        ExecTimeSpotFunc();

                        #endregion
                    }

                    Thread.Sleep(1000 * 60); //每分钟
                }
            }
            catch (Exception ex)
            {
                log.WriteLog("异常：定点执行（时间点）");
                log.WriteExceptionLog(ex);
            }
        }


        #region 执行业务代码

        /// <summary>
        /// ExecIntervalSecondFunc
        /// </summary>
        private void ExecIntervalSecondFunc()
        {
            //数据库操作
            //DataTable dt = systemidao.GetDataTableBySQL(new StringBuilder($@"SELECT * FROM dbo.T_Project"));
            //log.WriteLog(JsonConvert.SerializeObject(dt));

            log.WriteLog("间隔执行（秒）");
        }

        /// <summary>
        /// ExecIntervalMinuteFunc
        /// </summary>
        private void ExecIntervalMinuteFunc()
        {
            log.WriteLog("间隔执行（分）");
        }

        /// <summary>
        /// ExecTimeSpotFunc
        /// </summary>
        private void ExecTimeSpotFunc()
        {
            log.WriteLog("定点执行（时间点）");
        }

        #endregion

    }
}
